// 题库问题相关类型定义
import type { BaseEntity, User, Tag, FileInfo, Category } from './index'

// 题目类型枚举
export enum QuestionType {
  SINGLE_CHOICE = 'single_choice',
  MULTIPLE_CHOICE = 'multiple_choice',
  TRUE_FALSE = 'true_false',
  FILL_BLANK = 'fill_blank',
  SHORT_ANSWER = 'short_answer',
  ESSAY = 'essay',
  MATCHING = 'matching',
  ORDERING = 'ordering',
  CODING = 'coding',
  OTHER = 'other'
}

// 题目难度枚举
export enum QuestionDifficulty {
  EASY = 'easy',
  MEDIUM = 'medium',
  HARD = 'hard',
  VERY_HARD = 'very_hard',
  CHALLENGE = 'challenge'
}

// 题目状态枚举
export enum QuestionStatus {
  DRAFT = 'draft',
  PENDING = 'pending',
  APPROVED = 'approved',
  REJECTED = 'rejected',
  ARCHIVED = 'archived',
  DELETED = 'deleted'
}

// 题目选项接口
export interface QuestionOption {
  id: string | number
  questionId: string | number
  content: string
  isCorrect?: boolean
  order: number
  explanation?: string
}

// 题目答案接口
export interface QuestionAnswer {
  id: string | number
  questionId: string | number
  answerContent?: string
  correctOptions?: string[] | number[]
  explanation?: string
  solution?: string
  scoringCriteria?: string
  maxScore: number
}

// 题目分类接口
export interface QuestionCategory {
  id: string | number
  name: string
  description?: string
  parentId?: string | number
  order: number
  questionCount: number
  createdAt: string
  updatedAt: string
}

// 答案结果接口
export interface AnswerResult {
  isCorrect: boolean
  score: number
  correctAnswer?: string | string[] | number | number[]
  explanation?: string
  timeSpent?: number
}

// 题目分页接口
export interface QuestionPagination {
  items: Question[]
  currentPage: number
  pageSize: number
  total: number
  totalPages: number
}

// 题目接口
export interface Question extends BaseEntity {
  content: string
  type: QuestionType | string
  difficulty: QuestionDifficulty | string
  status: QuestionStatus | string
  creatorId: string | number
  creator?: User
  category?: Category
  tags?: Tag[]
  chapterId?: string | number
  courseId?: string | number
  moduleId?: string | number
  groupId?: string | number
  options?: QuestionOption[]
  answer?: QuestionAnswer
  referenceMaterial?: string
  source?: string
  estimatedTime?: number
  maxScore: number
  usedCount: number
  correctRate?: number
  averageScore?: number
  isPublic: boolean
  isOfficial?: boolean
  imageUrl?: string
  isHot?: boolean
  isRecommended?: boolean
  media?: FileInfo[]
  metadata?: Record<string, any>
}

// 题目搜索参数接口
export interface QuestionSearchParams {
  keyword?: string
  type?: QuestionType | string
  difficulty?: QuestionDifficulty | string
  status?: QuestionStatus | string
  creatorId?: string | number
  categoryId?: string | number
  tagIds?: string[]
  chapterId?: string | number
  courseId?: string | number
  moduleId?: string | number
  groupId?: string | number
  isPublic?: boolean
  isOfficial?: boolean
  isHot?: boolean
  isRecommended?: boolean
  usedCountMin?: number
  usedCountMax?: number
  correctRateMin?: number
  correctRateMax?: number
  sortBy?: string
  sortOrder?: 'asc' | 'desc'
  startDate?: string
  endDate?: string
  page?: number
  pageSize?: number
}

// 创建题目接口
export interface CreateQuestion {
  content: string
  type: QuestionType | string
  difficulty: QuestionDifficulty | string
  categoryId?: string | number
  tagIds?: string[]
  chapterId?: string | number
  courseId?: string | number
  moduleId?: string | number
  groupId?: string | number
  options?: Omit<QuestionOption, 'id' | 'questionId'>[]
  answer?: Omit<QuestionAnswer, 'id' | 'questionId'>
  referenceMaterial?: string
  source?: string
  estimatedTime?: number
  maxScore: number
  isPublic?: boolean
  isOfficial?: boolean
  media?: FileInfo[]
  metadata?: Record<string, any>
}

// 更新题目接口
export interface UpdateQuestion {
  content?: string
  type?: QuestionType | string
  difficulty?: QuestionDifficulty | string
  status?: QuestionStatus | string
  categoryId?: string | number
  tagIds?: string[]
  chapterId?: string | number
  courseId?: string | number
  moduleId?: string | number
  groupId?: string | number
  options?: (QuestionOption | Omit<QuestionOption, 'id' | 'questionId'>)[]
  answer?: Partial<QuestionAnswer>
  referenceMaterial?: string
  source?: string
  estimatedTime?: number
  maxScore?: number
  isPublic?: boolean
  isOfficial?: boolean
  isHot?: boolean
  isRecommended?: boolean
  media?: FileInfo[]
  metadata?: Record<string, any>
}

// 题目统计接口
export interface QuestionStats {
  questions: number
  byType: Record<QuestionType | string, number>
  byDifficulty: Record<QuestionDifficulty | string, number>
  byStatus: Record<QuestionStatus | string, number>
  usedCount: number
  averageCorrectRate: number
  averageScore: number
  createdPerDay: {
    date: string
    count: number
  }[]
}

// 题库接口
export interface QuestionBank {
  id: string | number
  name: string
  description?: string
  creatorId: string | number
  creator?: User
  questionCount: number
  viewCount: number
  useCount: number
  isPublic: boolean
  isOfficial?: boolean
  isHot?: boolean
  isRecommended?: boolean
  coverImage?: string
  createdAt: string
  updatedAt: string
  questions?: Question[]
  tags?: Tag[]
  metadata?: Record<string, any>
}

// 题库搜索参数接口
export interface QuestionBankSearchParams {
  keyword?: string
  creatorId?: string | number
  isPublic?: boolean
  isOfficial?: boolean
  isHot?: boolean
  isRecommended?: boolean
  sortBy?: string
  sortOrder?: 'asc' | 'desc'
  startDate?: string
  endDate?: string
  page?: number
  pageSize?: number
}

// 创建题库接口
export interface CreateQuestionBank {
  name: string
  description?: string
  questionIds?: string[] | number[]
  isPublic?: boolean
  isOfficial?: boolean
  coverImage?: string
  tags?: string[]
  metadata?: Record<string, any>
}

// 更新题库接口
export interface UpdateQuestionBank {
  name?: string
  description?: string
  questionIds?: string[] | number[]
  isPublic?: boolean
  isOfficial?: boolean
  isHot?: boolean
  isRecommended?: boolean
  coverImage?: string
  tags?: string[]
  metadata?: Record<string, any>
}

// 题目收藏接口
export interface QuestionCollection {
  id: string | number
  userId: string | number
  user?: User
  questionId: string | number
  question?: Question
  collectionDate: string
  notes?: string
  folderId?: string | number
  folder?: QuestionFolder
}

// 题目文件夹接口
export interface QuestionFolder {
  id: string | number
  userId: string | number
  user?: User
  name: string
  description?: string
  parentId?: string | number
  parent?: QuestionFolder
  questionCount: number
  folderCount: number
  createdAt: string
  updatedAt: string
}

// 题目使用记录接口
export interface QuestionUsage {
  id: string | number
  questionId: string | number
  question?: Question
  userId: string | number
  user?: User
  attemptId?: string | number
  paperId?: string | number
  quizId?: string | number
  isCorrect: boolean
  score?: number
  answerGiven?: string | string[] | number | number[]
  timeSpent?: number
  timestamp: string
  ipAddress?: string
  device?: string
}

// 题目报告接口
export interface QuestionReport {
  id: string | number
  questionId: string | number
  question?: Question
  reporterId: string | number
  reporter?: User
  reason: string
  description?: string
  status: 'pending' | 'processing' | 'resolved' | 'dismissed'
  createdAt: string
  resolvedAt?: string
  resolvedByUserId?: string | number
  resolvedByUser?: User
}

// 题目分析接口
export interface QuestionAnalytics {
  questionId: string | number
  question?: Question
  totalAttempts: number
  correctAttempts: number
  correctRate: number
  averageScore: number
  averageTimeSpent: number
  difficultyLevel?: number
  discriminationIndex?: number
  pValue?: number
  pointBiserial?: number
  optionAnalysis?: {
    optionId: string | number
    selectedCount: number
    selectedPercentage: number
    isCorrect: boolean
  }[]
  performanceByUser?: {
    userId: string | number
    userName?: string
    isCorrect: boolean
    score: number
    timeSpent: number
  }[]
  trendAnalysis: {
    date: string
    attempts: number
    correctAttempts: number
    correctRate: number
  }[]
  difficultyTrend: {
    date: string
    difficulty: number
  }[]
}

// 试卷接口
export interface QuestionPaper {
  id: string | number
  name: string
  description?: string
  creatorId: string | number
  creator?: User
  totalQuestions: number
  totalScore: number
  estimatedTime?: number
  isPublic: boolean
  isTemplate?: boolean
  coverImage?: string
  createdAt: string
  updatedAt: string
  questions?: Question[]
  tags?: Tag[]
  metadata?: Record<string, any>
}

// 测验接口
export interface Quiz {
  id: string | number
  name: string
  description?: string
  creatorId: string | number
  creator?: User
  courseId?: string | number
  startTime?: string
  endTime?: string
  duration?: number
  passingScore?: number
  attemptLimit?: number
  isPublic: boolean
  isActive: boolean
  totalQuestions: number
  totalScore: number
  createdAt: string
  updatedAt: string
  questions?: Question[]
  tags?: Tag[]
  metadata?: Record<string, any>
}

// 章节接口
export interface Chapter {
  id: string | number
  name: string
  description?: string
  courseId: string | number
  order: number
  parentId?: string | number
  parent?: Chapter
  questionCount: number
  createdAt: string
  updatedAt: string
  subChapters?: Chapter[]
  questions?: Question[]
}